package com.chen.project.white_jotter.controller;


import com.chen.project.white_jotter.pojo.AdminUserRole;
import com.chen.project.white_jotter.pojo.User;
import com.chen.project.white_jotter.result.Result;
import com.chen.project.white_jotter.service.UserService;
import com.chen.project.white_jotter.service.admin.AdminUserRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/admin")
public class AdminUserController {

    @Resource
    private UserService userService;
    @Resource
    private AdminUserRoleService adminUserRoleService;

    // 获取一页用户
    @GetMapping("/user")
    public Result getPageUser(Integer pageNum, Integer pageSize) {

        Map<String, Object> userMap = userService.getPageUser(pageNum, pageSize);
        return new Result(200, "成功获取一页用户", userMap);
    }

    // 添加用户
    @PostMapping("/user")
    public Result addUser(@RequestBody User requestUser) {

        int i = userService.addUser(requestUser);
        if (i > 0)
            return new Result(201, "添加用户成功");
        else
            return new Result(500, "添加用户失败");
    }

    // 改变用户状态
    @PutMapping("/user/status/{pageNum}")
    public Result updateUserStatus(@RequestBody User requestUser, @PathVariable("pageNum") Integer pageNum) {

        userService.updateUserStatus(requestUser, pageNum);
        return new Result(200, "用户状态更新成功");
    }

    // 修改用户信息
    @PutMapping("/user/{pageNum}")
    public Result editUser(@RequestBody User requestUser, @PathVariable("pageNum") Integer pageNum) {

        int edit = userService.editUser(requestUser, pageNum);
        return new Result(200, "修改用户信息成功");
    }

    // 修改用户角色
    @PutMapping("/user/allotRole/{userId}/{pageNum}/{newRoleId}")
    public Result allotRole(@PathVariable("userId") Integer userId, @PathVariable("pageNum") Integer pageNum, @PathVariable("newRoleId") Integer newRoleId) {

        int allot = userService.allotRole(userId, pageNum, newRoleId);
        return new Result(200, "修改成功");
    }

    // 删除用户
    @DeleteMapping("/user")
    public Result remove(Integer id) {

        userService.remove(id);
        return new Result(200, "删除成功");
    }

    // // 重置用户密码
    // @PutMapping("/password")
    // public Result resetPassword(@RequestBody User requestUser) {
    //     userService.resetPassword(requestUser);
    //     return new Result(200, "重置密码成功");
    // }


}
